package com.jfl.service.impl.system;

import java.util.List;

import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.jfl.base.BaseServiceImpl;
import com.jfl.common.MessageKey;
import com.jfl.config.Resources;
import com.jfl.exception.BusinessException;
import com.jfl.mapper.SysRoleMapper;
import com.jfl.mapper.SysRoleMenuMapper;
import com.jfl.model.SysRole;
import com.jfl.model.SysRoleMenu;
import com.jfl.system.SysRoleService;
import com.jfl.utils.ConvertUtil;

/**
 * 角色
 *  
 * @author  赵炎
 * @version  [V1.00, 2019年1月4日]
 * @since V1.00
 */
@Component
@Service(version="${jfl.version}")
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole, SysRoleMapper> implements SysRoleService
{

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    
    @Override
    public List<SysRole> selectRolesByUserId(Long userId)
    {
        List<Long> userRoles = this.mapper.selectRolesByUserId(userId);
        List<SysRole> roles = this.mapper.selectByMap(Maps.newHashMap());
        for (SysRole sysRole : roles)
        {
            if (userRoles.contains(sysRole.getId()))
            {
                sysRole.setFlag(Boolean.TRUE);
            }
        }
        return roles;
    }
    
    @Override
    public String checkRoleNameUnique(SysRole role)
    {
        Long id = role.getId() == null ? -1L : role.getId();
        SysRole record = this.mapper.selectByRoleName(role.getRoleName());
        return result(record, id);
    }
    @Transactional
    @Override
    public int addRole(SysRole sysRole)
    {
        // 新增角色
        update(sysRole);
        // 新增角色菜单表
        inserRoleMenu(sysRole);
        return 0;
    }
    
    @Transactional
    @Override
    public int updateRole(SysRole sysRole)
    {
        // 新增角色
        update(sysRole);
        // 删除角色菜单
        this.sysRoleMenuMapper.deleteByRoleId(sysRole.getId());
        // 新增角色菜单表
        inserRoleMenu(sysRole);
        return 0;
    }
    private void inserRoleMenu(SysRole sysRole)
    {
        SysRoleMenu roleMenu = null;
        for (Long menuId : sysRole.getMenuIds())
        {
            roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(sysRole.getId());
            roleMenu.setMenuId(menuId);
            this.sysRoleMenuMapper.insert(roleMenu);
        }
    }
    
    @Transactional
    @Override
    public void deleteByIds(String ids) throws BusinessException
    {
        Long[] arr = ConvertUtil.toLongArray(ids);
        for (Long id : arr)
        {
            SysRole role = this.mapper.selectById(id);
            if (role !=null)
            {
                int count = this.sysRoleMenuMapper.countByRoleId(id);
                if(count > 0)
                {
                    throw new BusinessException(String.format(Resources.getMessage(MessageKey.MESSAGE_FORMAT), role.getRoleName()));
                }
            }
            this.mapper.deleteById(id);
        }
        
    }
}
